package cn.xiongxf.xhr;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

public class Producer {
    public static void main(String[] args) throws Exception {
        // 1.实例化消息生产者Producer
        DefaultMQProducer producer = new DefaultMQProducer("xhr_producer_group");
        // 2.设置NameServer的地址
        producer.setNamesrvAddr("localhost:9876");
        // 3.启动生产者
        producer.start();
        System.out.println("生产者启动。。。");
        // 4.发送消息到一个Broker
        Message msg = new Message("Topic_1", "Part_1", "你好，RocketMQ!".getBytes(RemotingHelper.DEFAULT_CHARSET));
        producer.send(msg, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("消信发送成功！");
                System.out.println(sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                throwable.getStackTrace();
                System.out.println("消息发送失败！");
            }
        });
        System.out.println("异步请求，可不是按代码顺序执行哦！");
        // 5.如果不再发送消息，关闭Producer实例
        //producer.shutdown();
    }
}